package com.mashibing.class10;

import java.util.Stack;

public class Code03_UnRecursiveTraversalBT {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }

    public static void pre(Node head) {
        System.out.print("pre-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.add(head);
            while (!stack.isEmpty()) {
                head = stack.pop(); // 头
                System.out.print(head.value + " ");
                if (head.right != null) { // 右
                    stack.push(head.right);
                }
                if (head.left != null) { // 左.
                    stack.push(head.left);
                }
                // 此时 栈中 从下到上的元素是 head.right  head.left
                // 等到下次循环弹栈的时候 是 先弹出 head.left.
            }
        }
        // 头 右 左的逆序 就是 左 右 头.
        System.out.println();
    }

    // 中序遍历
    public static void in(Node cur) {
        System.out.print("in-order: ");
        if (cur != null) {
            Stack<Node> stack = new Stack<Node>();
            while (!stack.isEmpty() || cur != null) {
                if (cur != null) { // 当前节点cur, cur头树, 整条左边界进栈, 直到遇到空, 切换为else分支.
                    stack.push(cur); // 将当前节点cur压栈.
                    cur = cur.left; // 当前节点切换为左节点.
                } else {
                    cur = stack.pop(); // 弹出当前节点, 打印, 将当前节点切换为右节点.
                    System.out.print(cur.value + " ");
                    cur = cur.right; // 当前节点切换为右节点.
                }
            }
        }
        System.out.println();
    }

    // 用两个栈实现后序遍历.
    public static void pos1(Node head) {
        System.out.print("pos-order: ");
        if (head != null) {
            Stack<Node> s1 = new Stack<Node>();
            Stack<Node> s2 = new Stack<Node>();
            s1.push(head);
            while (!s1.isEmpty()) {
                head = s1.pop(); // 头 右 左
                s2.push(head);
                if (head.left != null) {
                    s1.push(head.left);
                }
                if (head.right != null) {
                    s1.push(head.right);
                }
            }
            // 上面的s1栈中所弹出的数据是 头 右 左
            // s2 弹出的顺序 是 s1的逆序即为 : 左 右 头
            while (!s2.isEmpty()) {
                System.out.print(s2.pop().value + " ");
            }
        }
        System.out.println();
    }

    // 用一个栈实现后序遍历.
    public static void pos2(Node h) {
        System.out.print("pos-order: ");
        if (h != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.push(h);
            Node c = null;
            while (!stack.isEmpty()) {
                c = stack.peek();
                if (c.left != null && h != c.left && h != c.right) {
                    stack.push(c.left);
                } else if (c.right != null && h != c.right) {
                    stack.push(c.right);
                } else {
                    System.out.print(stack.pop().value + " ");
                    h = c;
                }
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        pre(head);
        System.out.println("========");
        in(head);
        System.out.println("========");
        pos1(head);
        System.out.println("========");
        pos2(head);
        System.out.println("========");
    }
}
